import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by sheep on 2016/1/27.
 */
public class E21_TreadCooperation {


    public static void main(String[] args) {
        Runnable coop1 = new Coop1();
        Runnable coop2 = new Coop2(coop1);
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.execute(coop1);
        exec.execute(coop2);
        Thread.yield();
        exec.shutdown();
    }
}


class Coop1 implements Runnable {
    public Coop1() {
        System.out.println("Constructed Coop1");
    }

    @Override
    public void run() {
        System.out.println("Coop1 going into wait");
        try {
            synchronized (this) {
                wait();
            }
        } catch (InterruptedException e) {

        }
        System.out.println("Coop1 exit wait");
    }
}

class Coop2 implements Runnable {
    Runnable task;

    public Coop2(Runnable task) {
        this.task = task;
        System.out.println("Constructed Coop2");
    }

    @Override
    public void run() {
        System.out.println("Coop2 pausing 5 seconds");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Coop2 calling notifyAll");
        synchronized (task) {
            task.notifyAll();
        }
    }
}